package org.example.rowing.service.impl;

import org.example.rowing.domain.Athlete;
import org.example.rowing.domain.Team;
import org.example.rowing.mapper.AthleteMapper;
import org.example.rowing.mapper.TeamMapper;
import org.example.rowing.service.DataImporter;
import org.example.rowing.service.TeamInfoService;
import org.example.rowing.vo.AthleteVO;
import org.example.rowing.vo.TeamVO;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AthleteDataImporter implements DataImporter<AthleteVO> {
    private TeamInfoService teamInfoService;  // 获取所属 Team

    public AthleteDataImporter(TeamInfoService teamInfoService) {
        this.teamInfoService = teamInfoService;
    }

    @Override
    public boolean validate(AthleteVO athlete) {
        // 验证 Athlete 数据
        if (athlete.getName() == null || athlete.getName().isEmpty()) {
            return false;  // 选手名称不能为空
        }
        if (athlete.getBirthDate() == null || athlete.getBirthDate().isEmpty()) {
            return false;  // 出生日期不能为空
        }
        if (athlete.getGenderEnum() == null) {
            return false;  // 性别不能为空
        }
        if (athlete.getTeam() == null || athlete.getTeam().getName() == null) {
            return false;
        }
        if (athlete.getEvent() == null || athlete.getEvent().getId() == null) {
            return false;
        }
        // 可以添加更多的验证逻辑
        return true;
    }

    public Athlete processForeignKeys(AthleteVO athleteVO) {
        Athlete athlete = new Athlete(athleteVO.getId(), null, athleteVO.getName(), athleteVO.getBirthDate(), athleteVO.getGenderEnum(), athleteVO.getCategory());
        // 处理外键关系：TeamId
        Team teamExample = new Team();
        teamExample.setName(athleteVO.getTeam().getName());
        teamExample.setEventId(athleteVO.getEvent().getId());
        List<TeamVO> teams = teamInfoService.showSearchedTeams(teamExample);
        if (teams != null && !teams.isEmpty()) {
            athlete.setTeamId(teams.get(0).getId());  // 根据 teamName 查找 team 并设置 teamId
        }
        return athlete;
    }

    @Override
    public Set<String> selectAliveNames() {
        return null;
    }
}

